﻿package com.eaglesandcobras.games.scenes
{
	import com.eaglesandcobras.games.scenes.GamePage;
	import com.gaiaframework.events.*;
	import com.gaiaframework.debug.*;
	import com.gaiaframework.api.*;
	import flash.display.*;
	import flash.events.*;
	import com.eaglesandcobras.common.data.XMLLoad;
	import com.greensock.loading.*;
	import com.greensock.events.LoaderEvent;
	import flash.utils.Timer;
	import com.google.analytics.GATracker;
	
	public class BasePage extends GamePage
	{	
		private var _scenes:XMLList;
		private var _params:Array;
		private var _xl:XMLLoad;
		private var _xmlPath:String = "xml/game.xml";
		private var _soundsPath:String = "xml/sounds.xml";
		private var _current:int = 0;
		private var _replay:int = 0;
		private var _win:Boolean;
		private var _sound:Boolean = true;
		private var _loops:Array = new Array();
		private var _totalScore:Number = 0;
		private var _scoreUpdates:Array = new Array();
		private var _soundUpdates:Array = new Array();
		private var _timerUpdates:Array = new Array();
		private var _copyUpdates:Array = new Array();
		private var _timeLeft:int = 0 ;
		private var _t:Timer;
		private var _tracker:GATracker;
		private var _trackVars:Object = new Object();
		
		public function BasePage()
		{
			super();
			LoaderMax.activate([MP3Loader]);
			loadXML();
			loadSounds();
		}
		
		/***********************
		LOADING ORDER
		************************/
		public function loadXML():void {
			_xl = new XMLLoad(_xmlPath, "scene");
			_xl.addEventListener(Event.COMPLETE, loaded);
		}
		
		private function loaded(e:Event):void {
			_xl.removeEventListener(Event.COMPLETE, loaded);
			_scenes = _xl.data;
			
			_params = new Array();
			
			for ( var i=0;i<_scenes.length();i++) {
				var list:Object = _scenes[i].item;
				var obj:Object = new Object;
				
				if (String ( _scenes[i].@tracking).length>0) addTracking(_scenes[i].@tracking);
				
				for ( var a in list ) {
					obj[list[a].@name] = list[a];
				}
				_params.push (obj);
			}
		}
		
		/***********************
		TRACKING
		************************/
		private function addTracking($account:String):void {
			_trackVars.account = $account;
			_trackVars.namePrefix = "Game/";
			_tracker = new GATracker(this, _trackVars.account, "AS3", false);
			_tracker.trackPageview(_trackVars.namePrefix);
		}
		
		public function googleTrack($type:String):void {
			if (_tracker) _tracker.trackEvent("GameEvent", $type);
		}
		
		/***********************
		LOADING SOUNDS
		************************/
		private function loadSounds():void {
			var _sl:XMLLoader = new XMLLoader( _soundsPath, {name:"soundsXML", onChildComplete:soundDone});
			_sl.load();
		}
		
		private function soundDone(e:LoaderEvent):void {
			if (e.target.vars.repeat=="-1") _loops.push ( e.target.vars.name );
		}
		
		/************************
		PLAY SOUND
		*************************/
		public function playGameSound($name:String, $v:Number=.5):void {
			if (LoaderMax.getLoader($name)&&_sound) {
				var ml:MP3Loader = LoaderMax.getLoader($name);
				ml.volume = $v;
				ml.gotoSoundTime(0);
				ml.playSound();
			}
		}
		
		/************************
		STOP SOUND
		*************************/
		public function stopGameSound($name:String):void {
			if (LoaderMax.getLoader($name)&&_sound) {
				var ml:MP3Loader = LoaderMax.getLoader($name);
				ml.pauseSound();
			}
		}
		
		/***********************
		GAME OVER. GO TO LAST. YOU LOST.
		************************/
		public function gameOver():void {
			_current = _scenes.length()-1;
			win = false;
			Gaia.api.goto( "base/overlay/"+ _scenes[_current].@type );
		}
		
		/***********************
		NEXT SCENE. YOU PASSED
		************************/
		public function passLevel():void {
			_current++;
			
			if (_current<_scenes.length() ) {
				win = true;
				Gaia.api.goto( "base/overlay/"+_scenes[_current].@type );
			
			} else {
				replayGame();
			}
			
		}
		
		/***********************
		REPLAY.
		************************/
		public function replayGame():void {
			_current = 0;
			
			for (var i=0;i<_scenes.length();i++) {
				if (_scenes[i].@replay == "true") _current=i;
			}
			
			resetScore();
			Gaia.api.goto( "base/overlay/"+_scenes[_current].@type );
		}
		
		/************************
		WIN OR LOSE?
		************************/
		public function set win($b:Boolean):void {
			_win = $b;
		}
		
		public function get win():Boolean {
			return _win;
		}
		
		/*********************************
		PARAM
		**********************************/
		public function getParams():Object {
			return _params[_current];
		}
		
		/*********************************
		GLOBAL SOUND
		**********************************/
		public function switchSound():void {
			if (_sound) {
				_sound = false;
				stopAllLoops();
			}
			else {
				_sound = true;
				startAllLoops();
			}
			
			updateSounds();
		}
		
		/*********************************
		FUNCTIONS TO BE CALLED ON SOUND UPDATES
		**********************************/
		public function addSoundUpdateFunction($function:Function):void {
			_soundUpdates.push ( $function );
		}
		
		public function updateSounds():void {
			for ( var i=0;i<_soundUpdates.length;i++) {
				_soundUpdates[i]();
			}
		}
		
		public function get globalSound():Boolean {
			return _sound;
		}
		
		/*********************************
		STOP ALL LOOPS
		**********************************/
		public function stopAllLoops():void {
			for ( var i=0;i<_loops.length;i++) {
				var ml:MP3Loader = LoaderMax.getLoader(_loops[i]);
				ml.pauseSound();
			}
		}
		
		/*********************************
		START ALL LOOPS
		**********************************/
		public function startAllLoops():void {
			for ( var i=0;i<_loops.length;i++) {
				var ml:MP3Loader = LoaderMax.getLoader(_loops[i]);
				ml.soundTime=0;
				ml.playSound();
			}
		}
		
		/*********************************
		SCORE
		**********************************/
		public function get totalScore():Number {
			return _totalScore;
		}
		
		public function addToScore($score:int):void {
			_totalScore += $score;
			if (_totalScore<0) _totalScore = 0;
			
			updateScores();
		}
		
		public function resetScore():void {
			_totalScore = 0;
			
			updateScores();
		}
		
		/*********************************
		FUNCTIONS TO BE CALLED ON SCORING UPDATES
		**********************************/
		public function addScoreUpdateFunction($function:Function):void {
			_scoreUpdates.push ( $function );
		}
		
		public function updateScores():void {
			for ( var i=0;i<_scoreUpdates.length;i++) {
				_scoreUpdates[i]();
			}
		}
		
		/*********************************
		TIMER
		**********************************/
		public function createATimer($length:int):void {
			_t = new Timer(1000, $length);
			
			_timeLeft = $length;
			updateTimes();
		}
		
		public function startATimer($function:Function=null):void {
			_t.addEventListener(TimerEvent.TIMER, timerHandler);
			if ($function!=null) _t.addEventListener(TimerEvent.TIMER_COMPLETE, $function);
			_t.start();
		}
		
		private function timerHandler(e:TimerEvent):void {
			_timeLeft--;
			updateTimes();
		}
		
		public function stopATimer():int {
			if (_t) _t.stop();
			
			var left:int = _timeLeft;
			_timeLeft = 0;
			updateTimes();
			
			return left;
		}
		
		public function get timeLeft():int {
			return _timeLeft;
		}
		
		
		/*********************************
		TIMER UPDATE FUNCTION
		**********************************/
		public function addTimeUpdateFunction($function:Function):void {
			_timerUpdates.push ( $function );
		}
		
		public function updateTimes():void {
			for ( var i=0;i<_timerUpdates.length;i++) {
				_timerUpdates[i]();
			}
		}
		
		/*********************************
		COPY UPDATE
		**********************************/
		public function addCopyUpdateFunction($function:Function):void {
			_copyUpdates.push ( $function );
		}
		
		public function updateCopy($string:String, $type:String=null):void {
			for ( var i=0;i<_copyUpdates.length;i++) {
				_copyUpdates[i]($string, $type);
			}
		}
		
	}
}
